<?php
class Admin_SlideshowController extends Apps_Controller_Action_Admin {

    public function galleryAction() {
        $rq = $this->_request;

        if($rq->getParam('act_delete_gallery')) {
            $this->_forward('delete-gallery', null, null);
        }

        $tbGallery = new Model_DbTable_Gallery();
        $dThumbnail	= $tbGallery->getListActive()->query()->fetchAll();

        if ($rq->isPost()) {

            $ids            = $this->_request->getParam('id');
            $linkTos        = $this->_request->getParam('link_to');
            $captions       = $this->_request->getParam('caption');
            $sortingNumbers = $this->_request->getParam('sorting_number');

            if(is_array($ids)) {
                foreach ($ids as $k => $id) {
                    $tbGallery->safelyUpdate(array('sorting_number' => $sortingNumbers[$k],
                                                   'link_to' 		=> $linkTos[$k],
                                                   'caption'		=> $captions[$k]),
                                            array('id =? ' => $id));
                }
            }

            if(Apps_Plugin_Upload_File::isUploaded('thumbnail')) {
                $uploader = new Apps_Plugin_Upload_File();
                $uploader->setMaxQuantity(1)
                         ->setSubCategory(Apps_Plugin_Upload_File::CAT_GALLERY);

                $thumbnail = ($uploaded = $uploader->upload())
                ? $uploaded['thumbnail']['uri']
                : null;

                $tbGallery->safelyInsert(array('path' 			=> $thumbnail,
                                                'sorting_number' => $rq->getParam('sorting_number'),
                                                'link_to' 		=> $rq->getParam('link_to'),
                                                'caption'		=> $rq->getParam('caption')));

                //resize images
                try {
                    $image = new Apps_Plugin_ResizeImages();
                    $image->load(PUBLIC_PATH .$thumbnail);
                    $image->resize(930, 455);
                    $image->save(PUBLIC_PATH .$thumbnail);
                } catch (Exception $e) {
                    //Excetion here
                }
            }

            $this->refresh();
        }
        $this->view->dataPhoto 	= $dThumbnail;
    }
    //delete gallery
    public function deleteGalleryAction() {
        $this->disallowBacktrack();
        $rq = $this->_request;
        $ids = $rq->getParam('item_checker');

        if(!$ids) {
            $this->_helper->redirector('gallery', null, null);
        } else {
            $tbGallery = new Model_DbTable_Gallery();
            foreach($ids as $v) {
                $gallery = $tbGallery->fetchRow(array('id =?' => $v));
                if($gallery) {
                    $tbGallery->delete(array('id =?' => $v));
                    $this->deletePhoto($gallery['path']);
                }
            }
        }
        $this->_helper->redirector('gallery', null, null);
    }
}